﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Yuebon.Commons.Encrypt;
using Yuebon.Commons.Helpers;
using Yuebon.Commons.Models;
using Yuebon.Shop.Application;
using Yuebon.Shop.IServices;
using Yuebon.Shop.Models;
using Yuebon.WebApi.Models;

namespace Yuebon.WebApi.Controllers
{
    /// <summary>
    /// 会员接口控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class MemberController : BaseController
    {
        private IMembersService membersService;
       
        /// <summary>
        /// 构造函数注入服务
        /// </summary>
        /// <param name="_iService"></param>
        public MemberController(IMembersService _iService)
        {
            membersService = _iService;
        }

        /// <summary>
        /// 会员注册
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="code">验证码</param>
        /// <param name="token">token令牌</param>
        /// <returns>返回用户Member对象</returns>
        [HttpGet("Register")]
        public IActionResult Register(string username, string password, string code, string token)
        {
            CommonResult result = new CommonResult();
            result = CheckToken(token);
            if (result.ErrCode == ErrCode.successCode)
            {
                if (string.IsNullOrEmpty(username))
                {
                    result.ErrMsg = "用户名不能为空！";
                }
                else if (string.IsNullOrEmpty(password))
                {
                    result.ErrMsg = "密码不能为空！";
                }
                else
                {
                    MemberGradesApp memberGradesApp = new MemberGradesApp();
                    Members membersTemp = membersService.GetMemberByUserName(username);
                    if (membersTemp == null)
                    {
                        Members members = new Members();
                        members.Id = GuidUtils.CreateNo();
                        members.UserName = username;
                        members.ReferralUserId = "";
                        members.GradeId = memberGradesApp.GetDefaultGrade().Id;
                        members.PasswordSalt = MD5Util.GetMD5_32(username + password).Substring(0, 16).ToLower();
                        members.Password = QQEncryptUtil.EncodePasswordWithVerifyCode(password, members.PasswordSalt);
                        members.CreateDate = DateTime.Now;
                        members.Gender = 2;
                        members.IsOpenBalance = false;
                        members.OrderNumber = 0;
                        members.Expenditure = 0;
                        members.Points = 0;
                        members.Balance = 0;
                        members.RequestBalance = 0;
                        members.TopRegionId = 0;
                        members.RegionId = 0;
                        members.CellPhoneVerification = false;
                        members.RegisteredSource = 1;
                        members.IsQuickLogin = false;
                        members.IsLogined = false;
                        long n = membersService.Insert(members);
                        if (n >= 0)
                        {
                            result.Success = false;
                            result.ErrMsg = ErrCode.err50003;
                            result.ErrCode = "50003";
                        }
                    }
                    else
                    {
                        result.Success = false;
                        result.ErrMsg = ErrCode.err50002;
                        result.ErrCode = "50002";
                    }
                }
            }
            else
            {
                result.Success = false;
            }
            return ToJsonContent(result);
        }
        /// <summary>
        /// 登录验证用户
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="code">验证码</param>
        /// <param name="token">token令牌</param>
        /// <returns>返回用户Member对象</returns>
        [HttpGet("Login")]
        public IActionResult Login(string username, string password, string code,string token)
        {
            CommonResult result = new CommonResult();
            result = CheckToken(token);
            if (result.ErrCode == ErrCode.successCode)
            {
                if (string.IsNullOrEmpty(username))
                {
                    result.ErrMsg = "用户名不能为空！";
                }
                else if (string.IsNullOrEmpty(password))
                {
                    result.ErrMsg = "密码不能为空！";
                }
                else
                {
                    MembersApp membersApp = new MembersApp();
                    Members members = membersApp.GetMemberByUserName(username);
                    if (members.Password == QQEncryptUtil.EncodePasswordWithVerifyCode(password, members.PasswordSalt)){
                        result.ResData = members;
                        result.Success = true;
                    }
                    else
                    {
                        result.ErrMsg = "登录密码不正确";
                        result.Success = false;
                    }
                }
            }
            else
            {
                result.Success = false;
            }
            return ToJsonContent(result);
        }
    }
}